﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace System.Web.Mvc.Html
{
    /// <summary>
    /// 上一页|下一页的分页方式的Html分页控件生成类
    /// </summary>
    public class HtmlPrevNextPaggerFormat : HtmlPaggerFormat
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpMethod">Http请求方式</param>
        public HtmlPrevNextPaggerFormat(HttpMethod httpMethod)
            : base(httpMethod)
        {
            this.FirstLabel = "first";
            this.PreviousLabel = "previous";
            this.NextLabel = "next";
            this.LastLabel = "last";
        }

        /// <summary>
        /// 构造函数，使用HttpGet请求方式
        /// </summary>
        public HtmlPrevNextPaggerFormat()
            : this(HttpMethod.GET)
        {

        }

        /// <summary>
        /// 首页按钮Label，默认值：first
        /// </summary>
        public string FirstLabel { get; set; }

        /// <summary>
        /// 上一页按钮Label,默认值：previous
        /// </summary>
        public string PreviousLabel { get; set; }

        /// <summary>
        /// 下一页按钮Label，默认值：next
        /// </summary>
        public string NextLabel { get; set; }

        /// <summary>
        /// 尾页按钮Label，默认值：last
        /// </summary>
        public string LastLabel { get; set; }

        /// <summary>
        /// 生成HttpGet方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public override string GeneratePostFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize)
        {
            int pageCount = pageSize != 0 ? (rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1) : 0;
            if (pageCount <= 1)
            {
                return String.Empty;
            }
            HttpRequest request = System.Web.HttpContext.Current.Request;
            UrlHelper urlHelper = new UrlHelper(request.RequestContext);
            string url = urlHelper.Action(actionName, controllerName, routeValues);
            url = urlHelper.Content(url);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<form action=\"" + url + "\" method=\"POST\" target=\"_self\">");
            if (String.IsNullOrEmpty(request["pageIndex"]))
            {
                sb.AppendLine("<input type=\"hidden\" name=\"pageIndex\" value=\"" + pageIndex + "\"/>");
            }
            if (request.Form.HasKeys())
            {
                foreach (string key in request.Form.Keys)
                {
                    sb.AppendLine("<input type=\"hidden\" name=\"" + key + "\" value=\"" + request[key] + "\"/>");
                }
            }
            if (pageIndex > 0)
            {
                sb.AppendLine("<input type=\"button\" value=\"" + this.FirstLabel + "\" onclick=\"$(this).siblings(':hidden[name=pageIndex]').val('0');$(this).parent().submit();\"/>");
                sb.AppendLine("<input type=\"button\" value=\"" + this.PreviousLabel + "\" onclick=\"$(this).siblings(':hidden[name=pageIndex]').val('" + (pageIndex - 1) + "');$(this).parent().submit();\"/>");
            }
            else
            {
                sb.AppendLine("<input type=\"button\" value=\"" + this.FirstLabel + "\" disabled=\"disabled\"/>");
                sb.AppendLine("<input type=\"button\" value=\"" + this.PreviousLabel + "\" disabled=\"disabled\"/>");
            }
            sb.AppendFormat("<span>{0}/{1}</span>", pageIndex + 1, pageCount);
            if (pageIndex < pageCount - 1)
            {
                sb.AppendLine("<input type=\"button\" value=\"" + this.NextLabel + "\" onclick=\"$(this).siblings(':hidden[name=pageIndex]').val('" + (pageIndex + 1) + "');$(this).parent().submit();\"/>");
                sb.AppendLine("<input type=\"button\" value=\"" + this.LastLabel + "\" onclick=\"$(this).siblings(':hidden[name=pageIndex]').val('" + (pageCount - 1) + "');$(this).parent().submit();\"/>");
            }
            else
            {
                sb.AppendLine("<input type=\"button\" value=\"" + this.NextLabel + "\" disabled=\"disabled\"/>");
                sb.AppendLine("<input type=\"button\" value=\"" + this.LastLabel + "\" disabled=\"disabled\"/>");
            }
            sb.AppendLine("</form>");

            return sb.ToString();
        }

        /// <summary>
        /// 生成HttpPost方式的分页控件
        /// </summary>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页显示个数</param>
        /// <returns></returns>
        public override string GenerateGetFormatString(string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize)
        {
            int pageCount = pageSize != 0 ? (rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1) : 0;
            if (pageCount <= 1)
            {
                return String.Empty;
            }

            StringBuilder sb = new StringBuilder();

            if (pageIndex > 0)
            {
                routeValues["pageIndex"] = 0;
                sb.AppendLine(this.Helper.ActionLink(this.FirstLabel, actionName, controllerName, routeValues, null).ToHtmlString());
                routeValues["pageIndex"] = pageIndex - 1;
                sb.AppendLine(this.Helper.ActionLink(this.PreviousLabel, actionName, controllerName, routeValues, null).ToHtmlString());
            }
            else
            {
                sb.AppendLine("<a href=\"#\">" + this.FirstLabel + "</a>");
                sb.AppendLine("<a href=\"#\">" + this.PreviousLabel + "</a>");
            }

            sb.AppendFormat("<span>{0}/{1}</span>", pageIndex + 1, pageCount);

            if (pageIndex < pageCount - 1)
            {
                routeValues["pageIndex"] = pageIndex + 1;
                sb.AppendLine(this.Helper.ActionLink(this.NextLabel, actionName, controllerName, routeValues, null).ToHtmlString());
                routeValues["pageIndex"] = pageCount - 1;
                sb.AppendLine(this.Helper.ActionLink(this.LastLabel, actionName, controllerName, routeValues, null).ToHtmlString());
            }
            else
            {
                sb.AppendLine("<a href=\"#\">" + this.NextLabel + "</a>");
                sb.AppendLine("<a href=\"#\">" + this.LastLabel + "</a>");
            }
            return sb.ToString();
        }
    }
}
